package com.gw.controller;

import com.gw.pojo.dto.PostSADTO;
import com.gw.pojo.entity.PostSA;
import com.gw.result.Result;
import com.gw.service.PostSAService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/postSA")
@Slf4j
public class PostSAController {
    @Autowired
    private PostSAService postService;

    @GetMapping("/getAll")
    public Result<List<PostSA>> getAll() {
        List<PostSA> allPosts = (List<PostSA>) postService.getAllPosts();
        return Result.success(allPosts);
    }

    @GetMapping("/getLatest/{limit}")
    public Result<List<PostSA>> getLatest(@PathVariable int limit) {
        List<PostSA> latestPosts = (List<PostSA>) postService.getLatestPosts(limit);
        return Result.success(latestPosts);
    }
    @PostMapping("/add")
    public Result add(
            @RequestBody @Validated PostSADTO addPostSADTO) {
        if(addPostSADTO.getAttachmentUrl()==null){
            throw new RuntimeException("attachmentUrl is null");
        }
        if(addPostSADTO.getPictureUrl()==null){
            throw new RuntimeException("pictureUrl is null");
        }

        // 添加帖子
        postService.addPost(addPostSADTO); // 假设返回帖子 ID

        // 返回成功信息，包含帖子 ID
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        postService.deletePost(id);
        return Result.success();
    }




}
